package com.lhj.xxsc.ware.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 罗惠江
 * @version 1.0
 * @email 1511619294@qq.com
 * @date 2021/1/3 17:12
 */
@Configuration
public class MyRabbitConfig {
//	RabbitTemplate rabbitTemplate;
//	public MyRabbitConfig(RabbitTemplate rabbitTemplate){
//		this.rabbitTemplate = rabbitTemplate;
//	}

	/**
	 * 使用JSON 序列化机制，进行消息转化
	 *
	 * @return
	 */
	@Bean
	public MessageConverter messageConverter() {
		return new Jackson2JsonMessageConverter();
	}

//	@RabbitListener(queues = "stock.release.stock.queue")
//	public void handle(Message message){
//
//	}

	@Bean
	public Exchange stockEventExchange() {
		// String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
		return new TopicExchange("stock-event-exchange", true, false);
	}

	@Bean
	public Queue stockReleaseStockQueue() {
		// String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
		return new Queue("stock.release.stock.queue", true, false, false);
	}

	@Bean
	public Queue stockDelayQueue() {
		// String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
		Map<String, Object> arguments = new HashMap<>();
		arguments.put("x-dead-letter-exchange", "stock-event-exchange");
		arguments.put("x-dead-letter-routing-key", "stock.release");
		arguments.put("x-message-ttl", 60000 *40);
		return new Queue("stock.delay.queue", true, false, false, arguments);
	}

	@Bean
	public Binding stockReleaseBinding() {
		// String destination, DestinationType destinationType, String exchange, String routingKey,
		//			Map<String, Object> arguments
		return new Binding("stock.release.stock.queue", Binding.DestinationType.QUEUE, "stock-event-exchange", "stock.release.#", null);
	}

	@Bean
	public Binding stockLockedBinding() {
		// String destination, DestinationType destinationType, String exchange, String routingKey,
		//			Map<String, Object> arguments
		return new Binding("stock.delay.queue", Binding.DestinationType.QUEUE, "stock-event-exchange", "stock.locked", null);
	}

//
//	/**
//	 * 自定义我们的rabbitTemplate
//	 */
//	// 在我们类对象创建完成后才来调用这个方法
//	@PostConstruct
//	public void initRabbitTemplate() {
//
//		// 消息到中间件回调
//		rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
//			/**
//			 *  只要消息抵达消息中间件，就会返回true
//			 * @param correlationData 当前数据关联的唯一id
//			 * @param ack 消息是否成功收到
//			 * @param cause 失败的原因
//			 */
//			@Override
//			public void confirm(CorrelationData correlationData, boolean ack, String cause) {
//				System.out.println("confirm==》 correlationData：" + correlationData + "; ack:" + ack + "; cause:" + cause + ";");
//			}
//		});
//
//		// 消息从交换机到队列回调
//		rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
//
//			/**
//			 * 消息没有投递给指定的队列才会回调
//			 * @param message 消息详情
//			 * @param replyCode 回复状态码
//			 * @param replyText 回复的文本内容
//			 * @param exchange 当时指定的交换机
//			 * @param routingKey 路由键
//			 */
//			@Override
//			public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
//				System.out.println("失败的消息==》 内容：" + "message： " + message + "replyCode： " + replyCode + "replyText： " + replyText + "exchange： " + exchange + "routingKey： " + routingKey + ";");
//			}
//		});
//	}


}

